import request from '@/utils/request'
import { getLocalChannels, hasToken, setLocalChannels } from '@/utils/storage'
import {
  SAVE_ALL_CHANNEL,
  SAVE_CHANNEL,
  SAVE_ARTICLELIST,
  SAVE_MORE_ARTICLE_LIST,
} from '../actionTypes/home'

/**
 * 获取频道列表
 * @returns
 */
export const getUserChannels = () => {
  return async (dispatch) => {
    // 登录了
    if (hasToken()) {
      const { data: res } = await request({
        method: 'GET',
        url: '/user/channels',
      })
      dispatch(saveUserChannels(res.channels))
      // 没登陆
    } else {
      const channels = getLocalChannels()
      // 本地有频道数据
      if (channels) {
        dispatch(saveUserChannels(channels))
        // 本地没有频道数据
      } else {
        // 获取默认频道数据并保存到本地
        const { data: res } = await request({
          method: 'GET',
          url: '/user/channels',
        })
        dispatch(saveUserChannels(res.channels))
        setLocalChannels(res.channels)
      }
    }
  }
}

/**
 * 保存频道列表到redux
 * @param {*} payload
 * @returns
 */
export const saveUserChannels = (payload) => {
  return {
    type: SAVE_CHANNEL,
    payload,
  }
}

/**
 * 获取所有频道
 * @returns
 */
export const getAllChannels = () => {
  return async (dispatch) => {
    const { data: res } = await request({
      method: 'GET',
      url: '/channels',
    })
    dispatch(saveAllChannels(res.channels))
  }
}

/**
 * 保存所有频道
 * @param {*} payload
 * @returns
 */
export const saveAllChannels = (payload) => {
  return {
    type: SAVE_ALL_CHANNEL,
    payload,
  }
}

/**
 * 删除频道
 * @param {Object} channel 点击频道的数据
 * @returns
 */
export const delChannel = (channel) => {
  return async (dispatch, getState) => {
    // 获取未删除前的频道数据
    const useChannels = getState().home.userChannels
    // 登录 发请求
    if (hasToken()) {
      await request({
        method: 'DELETE',
        url: '/user/channels/' + channel.id,
      })
      dispatch(
        saveUserChannels(useChannels.filter((item) => item.id !== channel.id))
      )
      // 未登录 修改本地
    } else {
      const result = useChannels.filter((item) => item.id !== channel.id)
      setLocalChannels(result)
      dispatch(saveUserChannels(result))
    }
  }
}

/**
 * 添加频道
 * @param {Object} channel 点击频道数据
 * @returns
 */
export const addChannel = (channel) => {
  return async (dispatch, getState) => {
    if (hasToken()) {
      await request.patch('/user/channels/', { channels: [channel] })
      dispatch(saveUserChannels([...getState().home.userChannels, channel]))
    } else {
      setLocalChannels([...getState().home.userChannels, channel])
      dispatch(saveUserChannels([...getState().home.userChannels, channel]))
    }
  }
}

/**
 * 获取文章列表
 * @param {*} channelId 文章id
 * @param {*} timestamp 第一次请求返回的时间戳
 * @returns
 */
export const getArticleList = (channelId, timestamp) => {
  return async (dispatch) => {
    const { data: res } = await request({
      method: 'get',
      url: '/articles',
      params: {
        timestamp: timestamp,
        channel_id: channelId,
      },
    })
    dispatch(
      setArticleList({
        channelId,
        list: res.results,
        timestamp: res.pre_timestamp,
      })
    )
  }
}

/**
 * 把文章列表保存到redux中
 * @param {*} payload
 * @returns
 */
export const setArticleList = (payload) => {
  return {
    type: SAVE_ARTICLELIST,
    payload,
  }
}

/**
 * 上拉获取新的列表追加到之前的列表后面
 * @param {*} channelId
 * @param {*} timestamp
 * @returns
 */
export const getMoreArticleList = (channelId, timestamp) => {
  return async (dispatch) => {
    const { data: res } = await request({
      method: 'get',
      url: '/articles',
      params: {
        timestamp: timestamp,
        channel_id: channelId,
      },
    })
    dispatch(
      setMoreArticleList({
        channelId,
        list: res.results,
        timestamp: res.pre_timestamp,
      })
    )
  }
}

export const setMoreArticleList = (payload) => {
  return {
    type: SAVE_MORE_ARTICLE_LIST,
    payload,
  }
}
